<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
    </head>
    <body>
        <div>TODO write content</div>
        <script src="http://wx.34537.com/Skin/Theme/Js/jquery_2.1.3.js" type="text/javascript" charset="utf-8"></script>


        <script type="text/javascript">
            // 测试微信分享
            wxShare('标题', '描述', '', 'http://wx.34537.com/Skin/Theme/images/agent/adtext.png');
            // 微信分享调用方法
            function wxShare(title, desc, linkUrl, imageUrl) {
                if(!linkUrl){
                    linkUrl=location.href.split('#')[0];
                }
                console.log(linkUrl);
                $.ajax({
                    type: "post",
                    async: false,
                    url: "http://wx.34537.com/publicservices/wxservice.aspx",
                    dataType: "jsonp",
                    jsonp: "cb", //传递给请求处理程序或页面的，用以获得jsonp回调函数名的参数名(一般默认为:callback)
                    jsonpCallback: "wxShareCallback", //自定义的jsonp回调函数名称，默认为jQuery自动生成的随机函数名，也可以写"?"，jQuery会自动为你处理数据
                    data: {action: 'jsapiconf', shareurl: linkUrl},
                    success: function (json) {
                        wxShareCallback(json, title, desc, linkUrl, imageUrl);
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        console.log(textStatus);
                        console.log(errorThrown);
                    }
                });
            }
            // jsonp回调方法
            function wxShareCallback(data, title, desc, linkUrl, imageUrl) {
                console.log(data);
                setWxShareConf(data, title, desc, linkUrl, imageUrl);
            }
            // 设置微信分享
            function setWxShareConf(conf, title, desc, linkUrl, imageUrl) {
                wx.config({
                    debug: false,
                    appId: conf.appid,
                    timestamp: conf.timestamp,
                    nonceStr: conf.noncestr,
                    signature: conf.signature,
                    jsApiList: ['checkJsApi', 'onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo', 'onMenuShareQZone']
                });
                var dataconf = {
                    title: title, // 分享标题
                    desc: desc, // 分享描述
                    link: linkUrl, // 分享链接
                    imgUrl: imageUrl // 分享图标
                };
                wx.ready(function () {
                    // 分享给朋友
                    wx.onMenuShareAppMessage({
                        title: dataconf.title,
                        desc: dataconf.desc,
                        link: dataconf.link,
                        imgUrl: dataconf.imgUrl
                    });
                    // 分享到朋友圈
                    wx.onMenuShareTimeline({
                        title: dataconf.title,
                        link: dataconf.link,
                        imgUrl: dataconf.imgUrl
                    });
                    // 分享到QQ
                    wx.onMenuShareQQ({
                        title: dataconf.title,
                        desc: dataconf.desc,
                        link: dataconf.link,
                        imgUrl: dataconf.imgUrl
                    });
                    // 分享到腾讯微博
                    wx.onMenuShareWeibo({
                        title: dataconf.title,
                        desc: dataconf.desc,
                        link: dataconf.link,
                        imgUrl: dataconf.imgUrl
                    });
                    // 分享到QQ空间
                    wx.onMenuShareQZone({
                        title: dataconf.title,
                        desc: dataconf.desc,
                        link: dataconf.link,
                        imgUrl: dataconf.imgUrl
                    });
                })
            }
        </script>
    </body>
</html>
